﻿using CommunityToolkit.Mvvm;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DLL测试;
using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Markup;
using System.Windows.Media;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;


namespace 模拟器远程控制
{
    public partial class MainWindowViewModel : ObservableObject
    {
        public MainWindowViewModel()
        {
            MotionDevice=new MotionDevice();
            Timer = new System.Timers.Timer();
            Timer.Interval = 100;
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();
            SelectMotionAxis = MotionDevice.Axes[0];
            MotionDevice.Axes[0].Selected = true;
            Ip = Properties.Settings1.Default.IP;
            Port = Properties.Settings1.Default.PORT;
        }

        private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            int i=0;
            double v=0;
            if(MotionDevice.ConnectState)
            {
                foreach(var temp in MotionDevice.Axes)
                {
                    if(MotionControl.GetPosition(handle, (short)temp.ID, ref i,ref v)>0)
                    {
                        temp.CurrentActualPosition = v;
                    }
                    if (MotionControl.GetSpeed(handle, (short)temp.ID, ref i, ref v) > 0)
                    {
                        temp.CurrentSpeed = v;
                    }
                }
            }
        }

        System.Timers.Timer Timer;

        [ObservableProperty]
        private IMotionDevice motionDevice;


        [ObservableProperty]
        private MotionAxis selectMotionAxis;


        private string ip="10.0.0.9";
        public string Ip
        {
            get { return ip; } set { SetProperty(ref ip, value); Properties.Settings1.Default.IP=ip; Properties.Settings1.Default.Save(); }
        }

        private string port="60310";
        public string Port
        {
            get { return port; } set { SetProperty(ref port, value); Properties.Settings1.Default.PORT = port; Properties.Settings1.Default.Save(); }
        }

        [ObservableProperty]
        private string info="";

        private int handle = 0;

        [RelayCommand]
        public void Select(object obj)
        {
            foreach(var temp in MotionDevice.Axes)
            {
                if(temp.Selected)
                {
                    SelectMotionAxis = temp;
                    return;
                }
            }
        }

        [RelayCommand]
        public void Sin(object obj)
        {
            if (MotionDevice.ConnectState)
            {
                if(MotionControl.SinRun(handle, (short)SelectMotionAxis.ID, SelectMotionAxis.Frequency, SelectMotionAxis.Amplitude, SelectMotionAxis.Count)>0)
                {
                    Info = SelectMotionAxis.Name + " 正弦运动";

                }
                else
                {
                    Info = SelectMotionAxis.Name + " 操作失败";

                }
            }
            else
            {
                Info = "未连接";
            }
        }

        [RelayCommand]
        public void Run(object obj)
        {
            

            if (MotionDevice.ConnectState)
            {
                if(MotionControl.MotionMoveToPos(handle, (short)SelectMotionAxis.ID, SelectMotionAxis.SetDisplayPosition, SelectMotionAxis.SetSpeed, 1)>0)
                {
                    Info = SelectMotionAxis.Name + " 运动";
                }
                else
                {
                    Info = SelectMotionAxis.Name + " 操作失败";
                }
            }
            else
            {
                Info = "未连接";
            }
        }

        [RelayCommand]
        public void SetBL(object obj)
        {
            float[] bl=new float[3];
            string[] temp = obj.ToString().Split(",");
            for(int i=0;i<temp.Length; i++)
            {
                bl[i] = float.Parse(temp[i]);   
            }
            MotionControl.MotionSetRatio(handle, bl);
        }

        [RelayCommand]
        public void Stop(object obj)
        {
            if (MotionDevice.ConnectState)
            {
                MotionControl.MotionStop(handle);
                Info = "停止";
            }
            else
            {
                Info = "未连接";
            }

        }

        [RelayCommand]
        public void ClearZero(object obj)
        {
            if (MotionDevice.ConnectState)
            {
                 MotionControl.ClearZero(handle);
                Info = "清零";
            }
            else
            {
                Info = "未连接";
            }
        }

        [RelayCommand]
        public void FindZero(object obj)
        {
            if(MotionDevice.ConnectState)
            {
                MotionControl.MotionBackZero(handle, 0);
                Info = "寻零";
            }
            else
            {
                Info = "未连接";
            }
        }


        [RelayCommand]
        public void Get(object obj)
        {
            if (MotionDevice.ConnectState)
            {
                MotionControl.MotionGetPrior(handle);
                Info = "获取控制权";
            }
            else
            {
                Info = "未连接";
            }
        }

        [RelayCommand]
        public void Connect(object obj)
        {
            Info = "";
            byte isowned=0;
            handle = MotionControl.MotionOpen(0, Ip, int.Parse(Port),ref isowned);
            if (handle > 0)
            {
                MotionDevice.ConnectState = true;
                Info = "连接成功";
                if(isowned==1)
                {
                    Info += "  正在被其他用户控制";
                }
                else if (isowned == 0)
                {
                    Info += "  拥有控制权限";
                }
                else
                {
                    Info += "  获取控制权限失败";
                }
            }
            else if(handle==-1)
            {
                MotionDevice.ConnectState = false;
                Info = "连接失败：超时";
            }
            else
            {
                //MotionDevice.ConnectState = false;
                //Info = "连接失败：正在被其他用户控制";
            }
        }

        [RelayCommand]
        public void DisConnect(object obj)
        {
            MotionControl.MotionClose(handle);
            MotionDevice.ConnectState = false;
            Info = "关闭成功";
        }
    }




}